package com.xuan.mail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.mail.domin.Groupm;
import com.xuan.mail.service.GroupmService;
import com.xuan.mail.mapper.GroupmMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 86156
* @description 针对表【group】的数据库操作Service实现
* @createDate 2024-03-17 13:14:39
*/
@Service
public class GroupmServiceImpl extends ServiceImpl<GroupmMapper, Groupm>
    implements GroupmService {
    @Resource
    GroupmMapper groupmMapper;

    @Override
    public List<Groupm> getGroup(Integer userId) {

        List<Groupm> list = groupmMapper.selectList(new LambdaQueryWrapper<Groupm>()
                        .eq(Groupm::getUserId, userId)
                        .or()
                        .eq(Groupm::getUserId,0)
                        .eq(Groupm::getIsdeleted, 0));
        System.out.println(list);
        return list;
    }

    @Override
    public void addGroup(Groupm groupm) {
        //查询是否有isdeleted为1的数据
        Groupm existingGroup = groupmMapper.selectOne(new LambdaQueryWrapper<Groupm>()
                .eq(Groupm::getIsdeleted, 1)
                .eq(Groupm::getUserId,groupm.getUserId())
                .eq(Groupm::getGroupName, groupm.getGroupName())
        );
        System.out.printf("existingGroup: %s", existingGroup);

        if (existingGroup!=null){

            //如果有，则修改isdeleted为0

            groupmMapper.update(new Groupm(),new LambdaUpdateWrapper<Groupm>()
                    .eq(Groupm::getGroupId,existingGroup.getGroupId())
                    .set(Groupm::getIsdeleted,0)
                    );


        }else {
            groupm.setIsdeleted(0);
            //添加
            groupmMapper.insert(groupm);
        }



    }

    @Override
    public void deleteGroup(Integer groupId) {
        //根据id删除
        groupmMapper.deleteById(groupId);

    }

    @Override
    public void logicDeleteGroup(Integer groupId) {
        //修改
        groupmMapper.update(new Groupm(),new LambdaUpdateWrapper<Groupm>()
                .eq(Groupm::getGroupId,groupId)
                .set(Groupm::getIsdeleted,1)
        );

    }

    @Override
    public void updateGroup(Groupm groupm) {
        groupmMapper.update(groupm,new LambdaUpdateWrapper<Groupm>()
                .eq(Groupm::getGroupId,groupm.getGroupId()));
    }

    @Override
    public Groupm getGroupById(Integer groupId) {
        LambdaQueryWrapper <Groupm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Groupm::getGroupId,groupId);
        queryWrapper.eq(Groupm::getIsdeleted,0);
        Groupm groupm = groupmMapper.selectOne(queryWrapper);
        return groupm;
    }
}




